package ${domain}.action.ajax.${moduleName};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${domain}.frame.base.Error;
import ${domain}.frame.excel.WExcel;
import ${domain}.frame.utils.LogUtil;
import ${domain}.frame.auth.LocalData;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.base.BaseResponse;
import ${domain}.frame.utils.ResponseUtil;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.frame.excel.exception.TemplateNotMatchException;
import ${domain}.module.${moduleName}.ent.${table.getCName()};
import ${domain}.module.${moduleName}.mgr.${table.getCName()}Manager;
<#if table.getCreate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}CreateRequest;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.req.${table.getCName()}DeleteRequest;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.req.${table.getCName()}FindRequest;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.req.${table.getCName()}GetRequest;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.req.${table.getCName()}SearchRequest;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}UpdateRequest;
</#if>
<#if table.getCreate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}CreateResponse;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}DeleteResponse;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}FindResponse;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}GetResponse;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}SearchResponse;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}UpdateResponse;
</#if>
<#list table.methods as item>
<#if item.selected>
import ${domain}.module.${moduleName}.req.${table.getCName()}${item.getAbbName()?default("")}Request;
import ${domain}.module.${moduleName}.rsp.${table.getCName()}${item.getAbbName()?default("")}Response;
</#if>
</#list>

import java.io.IOException;
import java.util.List;

public class ${table.getCName()}Ajax{

    @Autowired
    private ${table.getCName()}Manager ${table.getFName()}Manager;
<#if table.getCreate()>

    public ${table.getCName()}CreateResponse create(${table.getCName()}CreateRequest request) {
        return ${table.getFName()}Manager.create(request, LocalData.getToken());
    }
</#if>
<#if table.getDelete()>

    public ${table.getCName()}DeleteResponse delete(${table.getCName()}DeleteRequest request) {
        return ${table.getFName()}Manager.delete(request, LocalData.getToken());
    }
</#if>
<#if table.getUpdate()>

    public ${table.getCName()}UpdateResponse update(${table.getCName()}UpdateRequest request) {
        return ${table.getFName()}Manager.update(request, LocalData.getToken());
    }
</#if>
<#if table.getFind()>

    public ${table.getCName()}FindResponse find(${table.getCName()}FindRequest request) {
        return ${table.getFName()}Manager.find(request, LocalData.getToken());
    }
</#if>
<#if table.getGet()>

    public ${table.getCName()}GetResponse get(${table.getCName()}GetRequest request) {
        return ${table.getFName()}Manager.get(request, LocalData.getToken());
    }
</#if>
<#if table.getSearch()>

    public ${table.getCName()}SearchResponse search(${table.getCName()}SearchRequest request) {
        return ${table.getFName()}Manager.search(request, LocalData.getToken());
    }
</#if>
<#list table.methods as item>
<#if item.selected>

    public ${table.getCName()}${item.getAbbName()?default("")}Response ${item.name}(${table.getCName()}${item.getAbbName()?default("")}Request request) {
        return ${table.getFName()}Manager.${item.name}(request, LocalData.getToken());
    }
</#if>
</#list>
<#if table.getFind() && table.getCreate()>

    public Object template(){
        return ResponseUtil.apply(new WExcel<>(${table.getCName()}.class));
    }

    public Object exports(${table.getCName()}FindRequest request) {
        ${table.getCName()}FindResponse response = ${table.getFName()}Manager.find(request, LocalData.getToken());
        if (response.hasError()) {
            return response;
        } else if (response.getTotalCount() == 0) {
            response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
            return response;
        }
        return ResponseUtil.apply(new WExcel<>(${table.getCName()}.class).addDatas(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WExcel sheet = new WExcel<>(${table.getCName()}.class).read(file.getBytes(), new WExcel.Processor<${table.getCName()}>() {
                @Override
                public List<String> exec(${table.getCName()} o, int index) {
                    ${table.getCName()}CreateRequest request = MapperUtil.map(o, ${table.getCName()}CreateRequest.class);
                    List<String> validate = ValidationUtil.validate(request);
                    if (validate == null || validate.size() == 0) {
                        ${table.getCName()}CreateResponse ${table.getFName()}CreateResponse = ${table.getFName()}Manager.create(request, LocalData.getToken());
                        if (${table.getFName()}CreateResponse.hasError()) {
                            for (Error error : ${table.getFName()}CreateResponse.getErrors()) {
                                validate.add(error.getMessage());
                            }
                        }
                    }
                    return validate;
                }
            });

            if (sheet.hasError()) {
                return ResponseUtil.apply(sheet.getBytes(), sheet.getName() + "-检查.xlsx");
            } else {
                return baseResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.dumpException(e);
            baseResponse.addError(ErrorType.BUSINESS_ERROR, "上传文件出错");
        } catch (TemplateNotMatchException e) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
        }
        return baseResponse;
    }
</#if>
}
